<template>
  <div class='container'>
    <el-card>
      <el-row>
        <el-col :span='18'>
          <el-form inline size="small" :model="form">
            <el-form-item label="学科名称">
              <el-input v-model="form.subjectName" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span='6' >
          <el-button style="float: right;" type="success" icon="el-icon-edit" size="small" @click="dialogVisible = true">新增学科</el-button>
        </el-col>
      </el-row>
      <el-alert
        :title="`数据一共 ${total} 条`"
        type="info"
        :closable="false"
        show-icon
        style="margin-bottom: 15px">
      </el-alert>
      <el-table :data="subjectsList">
        <el-table-column label="序号" type="index" :index="indexMethod"/>
        <el-table-column label="学科名称" prop="subjectName" />
        <el-table-column label="创建者" prop="username" />
        <el-table-column label="创建日期">
          <template v-slot="{ row }">
            {{ row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template v-slot="{ row }">
            {{ row.isFrontDisplay === 1 ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column label="二级目录" prop="twoLevelDirectory" />
        <el-table-column label="标签" prop="tags" />
        <el-table-column label="题目数量" prop="totals" />
        <el-table-column label="操作" width="240px">
          <template v-slot="{ row }">
            <el-button type="text" @click="subjectClassification(row)">学科分类</el-button>
            <el-button type="text" @click="subjectLabel(row)">学科标签</el-button>
            <el-button type="text" @click="editShow(row)">修改</el-button>
            <el-button type="text" @click="del(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        background
        layout="prev, pager, next, sizes, jumper"
        :current-page="form.page"
        :total="total"
        :page-sizes="[10,15,20,25]"
        @current-change="currentChange"
        @size-change='sizeChange'>
      </el-pagination>
    </el-card>
    <SubjectsAdd ref="SubjectsAdd" :dialogVisible.sync="dialogVisible" @page="form.page = 1" @getSubjectsList='getSubjectsList'></SubjectsAdd>
  </div>
</template>

<script>
import SubjectsAdd from '../components/subjects-add'
import { list as getSubjectsList, add, update, remove } from '@/api/hmmm/subjects'
export default {
  name: 'subjects',
  components: {
    SubjectsAdd
  },
  data () {
    return {
      form: {
        subjectName: null,
        pagesize: 10,
        page: 1
      },
      subjectsList: [],
      total: 0,
      dialogVisible: false,
      value: true
    }
  },
  created () {
    this.getSubjectsList()
  },
  methods: {
    indexMethod (index) {
      return index + (this.form.page - 1) * this.form.pagesize + 1
    },
    async getSubjectsList () {
      const { data } = await getSubjectsList(this.form)
      console.log(data)
      this.subjectsList = data.items
      this.total = data.counts
    },
    currentChange (page) {
      this.form.page = page
      this.getSubjectsList()
    },
    sizeChange (pagesize) {
      this.form.page = 1
      this.form.pagesize = pagesize
      this.getSubjectsList()
    },
    search () {
      this.form.page = 1
      this.getSubjectsList()
    },
    clear () {
      this.form.subjectName = null
    },
    async add () {
      try {
        await this.$refs.dialogForm.validate()
      } catch {
        return false
      }
      if (this.dialogForm.id) {
        await update(this.dialogForm)
        this.$message.success('修改学科成功')
      } else {
        await add(this.dialogForm)
        this.form.page = 1
        this.$message.success('新增学科成功')
      }
      this.getSubjectsList()
      this.dialogVisible = false
    },
    editShow (row) {
      // console.log(row)
      this.dialogVisible = true
      this.$refs.SubjectsAdd.dialogForm = { ...row }
    },
    async del (row) {
      try {
        await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
          type: 'warning'
        })
      } catch {
        return false
      }
      if (this.subjectsList.length === 1) {
        this.form.page--
      }
      await remove(row)
      this.$message.success('删除学科成功')
      this.getSubjectsList()
    },
    subjectClassification (row) {
      const id = row.id
      const name = row.subjectName
      this.$router.push({ path: 'directorys', query: { id, name } })
    },
    subjectLabel (row) {
      const id = row.id
      const name = row.subjectName
      this.$router.push({ path: 'tags', query: { id, name } })
    }
  }
}
</script>

<style scoped lang='scss'>
.container{
  padding: 10px 15px;
  .pagination{
    float: right;
    margin: 15px 0;
  }
}
</style>
